package com.csp.mingyue.common.log.event;

import cn.hutool.core.bean.BeanUtil;
import com.csp.mingyue.system.api.entity.SysLoginLog;
import com.csp.mingyue.system.api.entity.SysOperateLog;
import com.csp.mingyue.system.api.feign.RemoteLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步调用日志服务
 *
 * @author Strive
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LogEventListener {

	private final RemoteLogService remoteLogService;

	/**
	 * 保存系统日志记录
	 */
	@Async
	@EventListener
	public void saveLog(OperateLogEvent operateLog) {
		remoteLogService.saveSysOperateLog(BeanUtil.copyProperties(operateLog, SysOperateLog.class));
	}

	@Async
	@EventListener
	public void saveLoginLog(LoginLogEvent loginLogEvent) {
		remoteLogService.saveLoginLog(BeanUtil.copyProperties(loginLogEvent, SysLoginLog.class));
	}

}
